package com.haiertvbic.timeline.weibo;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import android.content.Context;
import android.os.Handler;

import com.haiertvbic.timeline.constants.TimeLineConstants;
import com.haiertvbic.tvnow.util.SLog;
import com.weibo.sdk.android.Weibo;
import com.weibo.sdk.android.WeiboException;
import com.weibo.sdk.android.WeiboParameters;
import com.weibo.sdk.android.net.AsyncWeiboRunner;
import com.weibo.sdk.android.net.RequestListener;

public class WeiBoHelper {

	private static String userInfor = null;

	/**
	 * 
	 * @param context
	 * @param handler
	 * @param weibo
	 * @param source
	 * @param file
	 * @param status
	 *            这里是分享的文本信息
	 * @param lon
	 * @param lat
	 * @throws WeiboException
	 */
	public static void upload(Context context, final Handler handler,
			Weibo weibo, String source, String file, String status, String lon,
			String lat) throws WeiboException {
		WeiboParameters bundle = new WeiboParameters();
		bundle.add("pic", file);
		bundle.add("source", source);
		bundle.add("status", status);
		bundle.add("access_token", AccessTokenKeeper.readAccessToken(context)
				.getToken());
		String url = "https://api.weibo.com/2/statuses/upload.json";
		AsyncWeiboRunner.request(url, bundle, "POST", new RequestListener() {

			@Override
			public void onIOException(IOException arg0) {
				// TODO Auto-generated method stub
				SLog.i("soul", "onIOException = " + arg0);
				handler.sendEmptyMessage(TimeLineConstants.WEIBO_SHARE_FAIL);
			}

			@Override
			public void onError(WeiboException arg0) {
				// TODO Auto-generated method stub
				SLog.i("soul", "onError = " + arg0);
				handler.sendEmptyMessage(TimeLineConstants.WEIBO_SHARE_FAIL);
			}

			@Override
			public void onComplete4binary(ByteArrayOutputStream arg0) {
				// TODO Auto-generated method stub
				SLog.i("soul", "onComplete4binary = " + arg0);
				handler.sendEmptyMessage(TimeLineConstants.WEIBO_SHARE_FAIL);
			}

			@Override
			public void onComplete(String arg0) {
				// TODO Auto-generated method stub
				SLog.i("soul", "onComplete = " + arg0);
				handler.sendEmptyMessage(TimeLineConstants.WEIBO_SHARE_SUCCESS);
			}
		});
	}

	public static String getUserInfo(Context context, Long uid) {
		final WeiboParameters bundle = new WeiboParameters();
		bundle.add("uid", uid);
		bundle.add("access_token", AccessTokenKeeper.readAccessToken(context)
				.getToken());
		String url = "https://api.weibo.com/2/users/show.json";
		AsyncWeiboRunner.request(url, bundle, "GET", new RequestListener() {

			@Override
			public void onIOException(IOException arg0) {
				// TODO Auto-generated method stub
				SLog.i("soul", "onIOException = " + arg0);
			}

			@Override
			public void onError(WeiboException arg0) {
				// TODO Auto-generated method stub
				SLog.i("soul", "onError = " + arg0);
			}

			@Override
			public void onComplete4binary(ByteArrayOutputStream arg0) {
				// TODO Auto-generated method stub
				SLog.i("soul", "onComplete4binary = " + arg0);
			}

			@Override
			public void onComplete(String arg0) {
				// TODO Auto-generated method stub
				SLog.i("soul", "onComplete = " + arg0);
				userInfor = arg0;
			}
		});
		return userInfor;
	}
}
